#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using WaveMaster.Holidays.Club.Domain;
using WaveMaster.Holidays.Club.Service;
using System.Collections;

#endregion

namespace WaveMaster.Holidays.Club.Web
{
    public partial class UserControlCheckInControl : UserControl
    {
        User m_current_user;
        MainForm m_mainForm;
        SeaPortService m_seaPortService;
        CheckInService m_bookingService;
        TripService m_tripService;

        public UserControlCheckInControl(string name, User currentUser, MainForm mainForm)
        {
            InitializeComponent();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
            m_tripService = new TripService();// (TripService)ServiceFactory.Instance().GetService(ServiceFactory.TRIP_SERVICE);
            m_seaPortService = new SeaPortService();// (SeaPortService)ServiceFactory.Instance().GetService(ServiceFactory.SEAPORT_SERVICE);
            m_bookingService = new CheckInService();// (CheckInService)ServiceFactory.Instance().GetService(ServiceFactory.BOOKING_SERVICE);
            timerClearResult.Tick += new EventHandler(timerClearResult_Tick);
            initComboDataSource();
            initButtonEvent();
            m_current_user = currentUser;
            m_mainForm = mainForm;
            this.Name = name;
        }

        void timerClearResult_Tick(object sender, EventArgs e)
        {
            textBoxMessage.Text = "";
            textBoxMessage.BackColor = System.Drawing.Color.Transparent;
            timerClearResult.Stop();
        }

        private void initButtonEvent()
        {
            toolBarButtonRefresh.Click += new EventHandler(toolBarButtonRefresh_Click);
          //  toolBarButtonPrintManifest.Click += new EventHandler(toolBarButtonPrintManifest_Click);
        }

        //void toolBarButtonPrintManifest_Click(object sender, EventArgs e)
        //{
        //    Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
        //    if (trip == null) return;
        //    PrintManifestForm pm = new PrintManifestForm(trip);
        //    pm.ShowDialog();
        //}

        void toolBarButtonRefresh_Click(object sender, EventArgs e)
        {
            //comboBoxDepartureSeaPort_SelectedIndexChanged(sender, e);
            comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
            textBoxPassport.Text = "";
            textBoxPassport.SelectAll();
        }

        private void initComboDataSource()
        {
            comboBoxDepartureSeaPort.DataSource = m_current_user.UserType == UserType.Administrators ? m_seaPortService.LoadAllSeaPort() : m_seaPortService.LoadAllActiveSeaPort();
        }

        void CheckIn()
        {
            try
            {
                if (isValidForm())
                {

                    Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
                    Passenger pass = m_bookingService.GetPassengerByTripAndPassport(textBoxPassport.Text, trip);
                    if (pass != null)
                    {
                        if (pass.m_passengerStatus == PassengerStatus.Booking)
                        {
                            if (pass.m_boardingPassCode == string.Empty)
                            {
                                int passengerCount = m_bookingService.GetLatestBoardingPassNumber(trip);
                                pass.m_boardingPassCode = passengerCount.ToString().PadLeft(3, '0') + trip.m_tripCode + trip.m_departureDate.ToString("ddMMyy");
                                m_bookingService.UpdatePassangerStatusAndBoardingPass(pass, PassengerStatus.CheckIn);
                            }
                            else 
                            {
                                m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.CheckIn);
                            }
                            Valid();
                        }
                        else
                        {
                            ValidBoarding();
                        }
                        comboBoxDepartureTrip_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        INValid();
                    }
                    timerClearResult.Start();
                }
            }
            catch (Exception x)
            {
                MessageBox.Show(x.Message, "Error Exception", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1);
            }
        }
        private void Valid()
        {
            textBoxMessage.Text = "VALID.";
            textBoxMessage.Text = String.Format("{0} VALID", textBoxPassport.Text);
            textBoxMessage.BackColor = Color.Green;
            textBoxMessage.ForeColor = Color.White;
            textBoxPassport.Text = "";
            textBoxPassport.SelectAll();
        }
        private void ValidBoarding()
        {
            textBoxMessage.Text = "VALID.";
            textBoxMessage.Text = String.Format("{0} ALREADY CHECK_IN", textBoxPassport.Text);
            textBoxMessage.BackColor = Color.Green;
            textBoxMessage.ForeColor = Color.White;
            textBoxPassport.Text = "";
            textBoxPassport.SelectAll();
        }
        private void INValid()
        {
            textBoxMessage.Text = "INVALID.";
            textBoxMessage.Text = String.Format("{0} INVALID", textBoxPassport.Text);
            textBoxMessage.BackColor = Color.Red;
            textBoxMessage.ForeColor = Color.White;
            textBoxPassport.Text = "";
            textBoxPassport.SelectAll();

        }
        private void clearErrorProvider()
        {
            errorProviderBoarding.SetError(comboBoxDepartureTrip, "");
        }
        private bool isValidForm()
        {
            clearErrorProvider();
            bool valid_b = textBoxPassport.Text != "";
            bool valid_a = comboBoxDepartureTrip.SelectedValue != null;
            if (!valid_a) errorProviderBoarding.SetError(comboBoxDepartureTrip, "Please choose Departure Trip");
            return valid_a && valid_b;
        }

        private void comboBoxDepartureSeaPort_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBoxDepartureTrip.DataSource = null;
            if (comboBoxDepartureSeaPort.SelectedValue == null) return;
            SeaPort port = (SeaPort)comboBoxDepartureSeaPort.SelectedValue;
            comboBoxDepartureTrip.DataSource = 
                m_tripService.LoadAllTripByDateAndSeaPort(dateTimePickerBoardingDate.Value, port.m_seaPortID);
        }

        private void comboBoxDepartureTrip_SelectedIndexChanged(object sender, EventArgs e)
        {
            listViewCheckIn.Items.Clear();
            listViewBoarding.Items.Clear();
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            if (trip == null) return;
            IList passCheckIn =
               m_current_user.UserType == UserType.Administrators ? 
               m_bookingService.GetPassengerByStatusAndTripAndCheckInstatus(PassengerStatus.Booking, trip, CheckInStatus.Booking)
               : m_bookingService.GetPassengerByStatusAndTripAndCheckInstatus(PassengerStatus.Booking, trip, CheckInStatus.Booking, m_current_user.TravelAgent);
            foreach (Passenger p in passCheckIn)
            {
                p.m_booking = m_bookingService.GetCheckInByID(p.m_booking.m_bookingID);
                ListViewItem item = listViewCheckIn.Items.Add(p.m_booking.m_bookingCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }
            IList passBoarding = 
                m_current_user.UserType == UserType.Administrators ? 
                m_bookingService.GetPassengerByStatusAndTripAndCheckInstatus(PassengerStatus.CheckIn, trip, CheckInStatus.Booking)
                : m_bookingService.GetPassengerByStatusAndTripAndCheckInstatus(PassengerStatus.CheckIn, trip, CheckInStatus.Booking, m_current_user.TravelAgent);
            foreach (Passenger p in passBoarding)
            {
                ListViewItem item = listViewBoarding.Items.Add(p.m_boardingPassCode);
                item.SubItems.Add(p.m_passportNo);
                item.SubItems.Add(p.m_name);
                item.Tag = p;
            }

        }

        private void menuItem1_Click_1(object sender, EventArgs e)
        {
            if (listViewCheckIn.SelectedItem == null) return;
            Passenger pass = (Passenger)listViewCheckIn.SelectedItem.Tag;
            if (pass == null) return;
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            if (pass.m_boardingPassCode == string.Empty)
            {
                int passengerCount = m_bookingService.GetLatestBoardingPassNumber(trip);
                pass.m_boardingPassCode = passengerCount.ToString().PadLeft(3, '0') + trip.m_tripCode + trip.m_departureDate.ToString("ddMMyy");
                m_bookingService.UpdatePassangerStatusAndBoardingPass(pass, PassengerStatus.CheckIn);
            }
            else
            {
                m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.CheckIn);
            }
            comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            if (listViewBoarding.SelectedItem == null) return;
            Passenger pass = (Passenger)listViewBoarding.SelectedItem.Tag;
            if (pass == null) return;
            Trip trip = (Trip)comboBoxDepartureTrip.SelectedValue;
            m_bookingService.UpdatePassangerStatus(pass, PassengerStatus.Booking);
            comboBoxDepartureTrip_SelectedIndexChanged(sender, e);
        }

        private void textBoxBoardingPass_TextChanged(object sender, EventArgs e)
        {
            if (m_txtBoardingPassKey == Keys.Enter)
            {
                CheckIn();
            }
        }

        Keys m_txtBoardingPassKey = Keys.A;

        private void textBoxBoardingPass_KeyDown(object objSender, KeyEventArgs objArgs)
        {
            m_txtBoardingPassKey = objArgs.KeyData;
        }

        private void dateTimePickerBoardingDate_ValueChangedQueued(object sender, EventArgs e)
        {
            comboBoxDepartureSeaPort_SelectedIndexChanged(sender, e);
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            if (listViewBoarding.SelectedItem == null) return;
            Passenger pass = (Passenger)listViewBoarding.SelectedItem.Tag;
            if (pass == null) return;
            PrintBoardingPassForm pb = new PrintBoardingPassForm(pass.m_passangerID, m_current_user, true);
            pb.ShowDialog();
        }

        private void buttonConfirmBooking_Click(object sender, EventArgs e)
        {
            menuItem1_Click_1(sender, e);
        }
    }
}